package com.lw.sbdemo2.common.vo;

import com.github.pagehelper.Page;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>标题: 分页插件Page封装</p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2018</p>
 * <p>公司: 智业软件</p>
 *
 * @author LinWang
 * @date 2018-05-09 11:55
 */
@Data
@NoArgsConstructor
@ApiModel(description = "分页实体")
public class PageInfo<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "当前页", name = "pageNum")
    private int pageNum;

    @ApiModelProperty(value = "每页的数量", name = "pageSize")
    private int pageSize = 10;

    @ApiModelProperty(value = "总记录数", name = "total", hidden = true)
    private long total;

    @ApiModelProperty(value = "总页数", name = "pages", hidden = true)
    private int pages;

    @ApiModelProperty(value = "结果集", name = "rows", hidden = true)
    private List<T> rows;

    @ApiModelProperty(value = "是否为第一页", name = "isFirstPage", hidden = true)
    private boolean isFirstPage = false;

    @ApiModelProperty(value = "是否为最后一页", name = "isLastPage", hidden = true)
    private boolean isLastPage = false;

    /**
     * 包装Page对象
     *
     * @param list
     */
    public PageInfo(List<T> list) {
        if (list instanceof Page) {

            Page page = (Page) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();

            this.pages = page.getPages();
            this.rows = page;
            this.total = page.getTotal();
        } else if (list instanceof Collection) {
            this.pageNum = 1;
            this.pageSize = list.size();

            this.pages = 1;
            this.rows = list;
            this.total = list.size();
        }
        if (list instanceof Collection) {
            judgePageBoudary();
        }
    }

    /**
     * 判定页面边界
     */
    private void judgePageBoudary() {
        isFirstPage = pageNum == 1;
        isLastPage = pageNum == pages;
    }
}
